import { constantRoutes } from '@/router'
import { getRouters } from '@/api/menu'
import Layout from '@/layout/index'
//import ParentView from '@/componetnts/ParentView'

const permission = {
    state: {
        routes:[],
        addRoutes: [],
        sidebarRouters: [],
    },

    mutations: {
        SET_ROUTES: (state, routes) => {
            state.addRoutes = routes
            state.routes = constantRoutes.concat(routes)
        },
        SET_SIDEBAR_ROUTERS: (state, routers) => {
            state.sidebarRouters = constantRoutes.concat(routers)
        },
    },

    actions: {
        GenerateRoutes({ commit }) {
            return new Promise(resolve => {
                getRouters().then(res => {
                    const sdata = JSON.parse(JSON.stringify(res.data))
                    const rdata = JSON.parse(JSON.stringify(res.data))
                    console.log(sdata)
                    const sidebarRoutes = filterAsyncRouter(sdata)
                    const rewriteRoutes = filterAsyncRouter(rdata, true)
                    rewriteRoutes.push({ path: '*', 
                                         redirect: '/404', 
                                         hidden: true }) 
                    console.log('sidebarRoutes')
                    console.log(sidebarRoutes)
                    commit('SET_ROUTES', rewriteRoutes)
                    commit('SET_SIDEBAR_ROUTERS', sidebarRoutes)
                    resolve(rewriteRoutes)
                })
            })
        }
    }
}

function filterAsyncRouter(asyncRouterMap, isRewrite = false) {
    return asyncRouterMap.filter(route => {
        if (isRewrite && route.children) {
            console.log('***1')
            route.children = filterChildren(route.children)
        }
        if (route.component) {
            console.log('***2')
            if (route.component === 'Layout') {
                route.component = Layout
            } else if (route.component === 'ParentView') {
                //route.component = ParentView
            } else {
                route.component = loadView(route.component)
            }
        }
        if (route.children != null 
                && route.children 
                && route.children.length) {
            route.children = filterAsyncRouter(route.children, 
                                               route, 
                                               isRewrite)
        }
        return true
    })
}

function filterChildren(childrenMap) {
    var children = []
    childrenMap.forEach((el) => {
        if (el.children && el.children.length) {
            if (el.component === 'ParentView') {
                el.children.forEach( c => {
                    c.path = el.path + '/' + c.path
                    if (c.children && c.children.length) {
                        children = children.concat(
                                    filterChildren(c.children,c))
                    }
                    children.push(c)
                })
                return
            }
        }
        children = children.concat(el)
    })
    return children
}

export const loadView = (view) => {
    console.log('----------------------------------')
    console.log(view)
    return (resolve) => require([`@/views/${view}`], resolve)
}

export default permission
